﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;
using Microsoft.Research.Kinect.Nui;
using Microsoft.Research.Kinect.Audio;
using Coding4Fun.Kinect.Wpf;


namespace kinectPaint
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        float scale = 1F;
        bool newStroke = true;
        SolidColorBrush myRedBrush = new SolidColorBrush(Colors.Red);
        SolidColorBrush myGreenBrush = new SolidColorBrush(Colors.Green);
        Runtime nui = new Runtime();
        StylusPointCollection pts1 = new StylusPointCollection();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            nui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking);

            //Must set to true and set after call to Initialize
            nui.SkeletonEngine.TransformSmooth = true;

            //Use to transform and reduce jitter
            var parameters = new TransformSmoothParameters
            {
                Smoothing = 0.7f,
                Correction = 0.00f,
                Prediction = 0.0f,
                JitterRadius = 0.05f,
                MaxDeviationRadius = 0.04f
            };

            nui.SkeletonEngine.SmoothParameters = parameters;

            nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);
            //pts1.Add(new StylusPoint(10,10));
            //Stroke stroke1 = new Stroke(pts1);
            //inkCanvas1.Strokes.Add(stroke1);
        }

        void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {   
            SkeletonFrame allSkeletons = e.SkeletonFrame;
            SkeletonData skeleton = (from s in allSkeletons.Skeletons
                                     where s.TrackingState == SkeletonTrackingState.Tracked
                                     select s).FirstOrDefault();
            if (skeleton.Joints[JointID.HandLeft].Position.X > skeleton.Joints[JointID.ElbowLeft].Position.X)
            {
                rightHand.Fill = myGreenBrush;
                if (newStroke)
                {
                    pts1 = new StylusPointCollection();
                    pts1.Add(new StylusPoint(skeleton.Joints[JointID.WristRight].ScaleTo(640, 480, scale, scale).Position.X,
                                             skeleton.Joints[JointID.WristRight].ScaleTo(640, 480, scale, scale).Position.Y));
                    inkCanvas1.Strokes.Add(new Stroke(pts1));
                    newStroke = false;
                    //inkCanvas1.Strokes[0].StylusPoints = pts1;
                }
                else
                {
                    pts1.Add(new StylusPoint(skeleton.Joints[JointID.WristRight].ScaleTo(640, 480, scale, scale).Position.X,
                                             skeleton.Joints[JointID.WristRight].ScaleTo(640, 480, scale, scale).Position.Y));
                    inkCanvas1.Strokes[inkCanvas1.Strokes.Count - 1].StylusPoints = pts1;
                }

                //inkCanvas1.Strokes.Draw();
            }
            else
            {
                newStroke = true;
                rightHand.Fill = myRedBrush;
            }
            SetEllipsePosition(rightHand,skeleton.Joints[JointID.WristRight]);
        }

        private void inkCanvas1_Gesture(object sender, InkCanvasGestureEventArgs e)
        {

        }

        private void SetEllipsePosition(FrameworkElement ellipse, Joint joint)
        {
            var scaledJoint = joint.ScaleTo(640, 480, scale, scale);
            Canvas.SetLeft(ellipse, scaledJoint.Position.X-10);
            Canvas.SetTop(ellipse, scaledJoint.Position.Y-10);
            
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            nui.Uninitialize();
        }
    }
}
